package org.serg.match3;

import android.content.Context;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class CustomSQLiteHelper extends SQLiteOpenHelper {

	  private static final String DATABASE_NAME = "mach3_players.db";
	  private static final int DATABASE_VERSION = 16;

	  // Database creation sql statement
	  private static final String PLAYERS_CREATE =
	    "create table Players (" 
	    + " _id integer primary key," 
	    + " name text not null,"
	    + " score integer not null"
	    + " );";
	  
	  public CustomSQLiteHelper(Context context) {
	    super(context, DATABASE_NAME, null, DATABASE_VERSION);
	  }
	  
	  private static CustomSQLiteHelper instance = null;
	  public static CustomSQLiteHelper getInstance(Context context) {
		  if (instance == null) {
			  instance = new CustomSQLiteHelper(context);
		  }
			return instance;
		}

	  public SQLiteDatabase open() throws SQLException {
	    return getWritableDatabase();
	  }

	  @Override
	  public void onCreate(SQLiteDatabase database) {
	    database.execSQL(PLAYERS_CREATE);
	    database.execSQL("insert into Players (_id, name, score) values (1, 'Troy Jones', 0); ");
		database.execSQL("insert into Players (_id, name, score) values (2, 'Barack Obama', 0); ");
		
	  }

	  @Override
	  public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
	    Log.w(CustomSQLiteHelper.class.getName(),
	        "Upgrading database from version " + oldVersion + " to "
	            + newVersion + ", which will destroy all old data");
	    db.execSQL("DROP TABLE IF EXISTS Players");
	    onCreate(db);
	  }
}
